package com.muqi.app.user.widget;

import com.muqi.app.qlearn.student.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class MenuWindow extends PopupWindow implements OnClickListener {
	private View conentView;
	private Context mContext;
	private Animation animation_in;
	private Animation animation_out;
	private Animation animation_top_in;
	private Animation animation_top_out;
	private int h;
	private int w;
	private ImageView iv_control;
	private ImageView iv_item_record;
	private ImageView iv_item_camera;
	private ImageView iv_item_ablum;
	private ImageView iv_item_body;
	private ImageView iv_top;

	public interface ClickMenuItemListener {
		void onClickClubWindowListener(int position);
	}

	private ClickMenuItemListener clickMenuItemListener;

	@SuppressWarnings("deprecation")
	public MenuWindow(final Activity context,
			ClickMenuItemListener clickMenuItemListener) {
		this.clickMenuItemListener = clickMenuItemListener;
		this.mContext = context;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.popup_menu, null);

		h = context.getWindowManager().getDefaultDisplay().getHeight();

		w = context.getWindowManager().getDefaultDisplay().getWidth();
		this.setContentView(conentView);
		this.setWidth(w);
		this.setHeight(h);
		this.setFocusable(true);
		this.setOutsideTouchable(false);
		ColorDrawable dw = new ColorDrawable(0000000000);
		// setAnimationStyle(R.style.popupwindow_anim_style);
		this.setBackgroundDrawable(dw);
		this.update();

		iv_control = (ImageView) conentView.findViewById(R.id.menu_control);

		iv_item_record = (ImageView) conentView
				.findViewById(R.id.menu_item_record);

		RelativeLayout.LayoutParams params = (LayoutParams) iv_item_record
				.getLayoutParams();
		params.width = w / 4;
		iv_item_record.setLayoutParams(params);

		iv_item_camera = (ImageView) conentView
				.findViewById(R.id.menu_item_camera);
		params = (LayoutParams) iv_item_camera.getLayoutParams();
		params.width = w / 4;
		iv_item_camera.setLayoutParams(params);

		iv_item_ablum = (ImageView) conentView
				.findViewById(R.id.menu_item_album);
		params = (LayoutParams) iv_item_ablum.getLayoutParams();
		params.width = w / 4;
		iv_item_ablum.setLayoutParams(params);

		iv_item_body = (ImageView) conentView.findViewById(R.id.menu_item_body);
		params = (LayoutParams) iv_item_body.getLayoutParams();
		params.width = w / 4;
		iv_item_body.setLayoutParams(params);

		iv_top = (ImageView) conentView.findViewById(R.id.iv_top);

		iv_control.setOnClickListener(this);
		iv_item_record.setOnClickListener(this);
		iv_item_camera.setOnClickListener(this);
		iv_item_ablum.setOnClickListener(this);
		iv_item_body.setOnClickListener(this);
		conentView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				closeAnimation();
				
			}
		});
		initAnimation();

	}

	/**
	 * 初始化一些动画
	 */
	private void initAnimation() {
		animation_in = AnimationUtils.loadAnimation(mContext, R.anim.menu_in);
		animation_out = AnimationUtils.loadAnimation(mContext, R.anim.menu_out);
		animation_top_in = AnimationUtils.loadAnimation(mContext,
				R.anim.menu_top_in);
		animation_top_out = AnimationUtils.loadAnimation(mContext,
				R.anim.menu_top_out);
		animation_out.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation arg0) {
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						MenuWindow.this.dismiss();

					}
				});

			}
		});

	}

	/**
	 * 设置打开的东动画
	 */
	private void openAnimation() {

		iv_item_record.startAnimation(animation_in);
		iv_item_camera.startAnimation(animation_in);

		iv_item_ablum.startAnimation(animation_in);
		iv_item_body.startAnimation(animation_in);
		iv_top.startAnimation(animation_top_in);

	}

	/**
	 * 设置关闭的动画
	 */
	private void closeAnimation() {
		// TODO Auto-generated method stub
		iv_item_record.startAnimation(animation_out);
		iv_item_camera.startAnimation(animation_out);
		iv_item_ablum.startAnimation(animation_out);
		iv_item_body.startAnimation(animation_out);
		iv_top.startAnimation(animation_top_out);
	}

	/**
	 * showPopupWidow
	 * 
	 * @param parent
	 */
	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// conentView.startAnimation(animation_in);
			openAnimation();
			this.showAtLocation(parent, Gravity.TOP, 0, 0);
		} else {
			// conentView.startAnimation(animation_out);
			//setOnDismissListener(null);
			closeAnimation();

		}
	}

	@Override
	public void onClick(View v) {
		if (clickMenuItemListener != null) {
			switch (v.getId()) {
			case R.id.menu_control:
				clickMenuItemListener.onClickClubWindowListener(0);
				break;
			case R.id.menu_item_record:
				clickMenuItemListener.onClickClubWindowListener(1);
				closeAnimation();
				break;
			case R.id.menu_item_camera:
				clickMenuItemListener.onClickClubWindowListener(2);
				closeAnimation();
				break;
			case R.id.menu_item_album:
				clickMenuItemListener.onClickClubWindowListener(3);
				closeAnimation();
				break;
			case R.id.menu_item_body:
				clickMenuItemListener.onClickClubWindowListener(4);
				closeAnimation();
				break;
			default:
				break;
			}
		}
		// MenuWindow.this.dismiss();
	}

}